package com.dmagic.cms.common.util;

import java.math.BigDecimal;

import org.apache.commons.lang3.math.NumberUtils;

public class NumberUtil extends NumberUtils {
    /**
     * >
     */
    public static final String RULE_BIG = ">";

    /**
     * >=
     */
    public static final String RULE_BIG_EQUAL = ">=";

    /**
     * =
     */
    public static final String RULE_EQUAL = "=";

    /**
     * @param num1
     * @param num2
     * @return num1*num2
     */
    public static Number multiply(Number num1, Number num2) {
        BigDecimal val1 = new BigDecimal(num1.doubleValue());
        BigDecimal val2 = new BigDecimal(num2.doubleValue());
        return val1.multiply(val2);
    }

    /**
     * @param num1
     * @param num2 value by which this BigDecimal is to be divided.
     * @param scale scale scale of the BigDecimal quotient to be returned.
     * @return num1/ num2
     */
    public static Number divide(Number num1, Number num2, int scale) {
        return divide(num1, num2, BigDecimal.ROUND_HALF_UP, scale);
    }

    /**
     * @param num1
     * @param num2 value by which this BigDecimal is to be divided.
     * @param roundType rounding mode to apply.
     * @param scale scale of the BigDecimal quotient to be returned.
     * @return num1/ num2
     */
    public static Number divide(Number num1, Number num2, int roundingMode, int scale) {
        if (num1.intValue() == 0 || num2.intValue() == 0) {
            return new BigDecimal(0);
        } else if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }

        BigDecimal val1 = new BigDecimal(String.valueOf(num1));
        BigDecimal val2 = new BigDecimal(String.valueOf(num2));
        BigDecimal val3 = null;

        if (roundingMode != 0) {
            val3 = val1.divide(val2, scale, roundingMode);
        }

        return val3;
    }

    /**
     * @description : For equality
     * 
     * @param a
     * @param b
     * @return
     */
    public static boolean equals(Number a, Number b) {
        if (a == null && b == null)
            return true;

        if (a != null && b != null)
            return a.doubleValue() == b.doubleValue();

        return false;
    }

    /**
     * @param a
     * @param b
     * @param rule
     * @return
     */
    public static boolean compare(Number a, Number b, String rule) {

        if (rule.equals(RULE_BIG_EQUAL)) {
            if (a == null || b == null)
                return false;

            return a.doubleValue() >= b.doubleValue();
        } else if (rule.equals(RULE_BIG)) {
            if (a == null || b == null)
                return false;

            return a.doubleValue() > b.doubleValue();
        } else if (rule.equals(RULE_EQUAL))
            return equals(a, b);

        return false;
    }

    /**
     * @description : BigDecimal to Long
     * 
     * @param decimal
     * @return
     */
    public static Long BigDecimal2Long(BigDecimal decimal) {
        Long result = null;
        if (decimal != null) {
            result = new Long(decimal.longValue());
        }
        return result;
    }

    /**
     * @description : BigDecimal to Double
     * 
     * @param decimal
     * @return
     */
    public static Double BigDecimal2Double(BigDecimal decimal) {
        Double result = null;

        if (decimal != null) {
            result = new Double(decimal.doubleValue());
        }

        return result;
    }

    /**
     * @description : BigDecimal to Integer
     * 
     * @param decimal
     * @return
     */
    public static Integer BigDecimal2Integer(BigDecimal decimal) {
        Integer result = null;
        if (decimal != null) {
            result = new Integer(decimal.intValue());
        }

        return result;
    }

    /**
     * @description : Description of the method
     * 
     * @author songlin.li
     * @param obj
     * @return
     */
    public static Integer toInteger(Object obj) {
        if (obj instanceof BigDecimal)
            return BigDecimal2Integer((BigDecimal) obj);
        return toInt(obj.toString(), 0);
    }

    /**
     * @description : Description of the method
     * 
     * @author songlin.li
     * @param obj
     * @return
     */
    public static Long toLong(Object obj) {
        if (obj instanceof BigDecimal)
            return BigDecimal2Long((BigDecimal) obj);
        return toLong(obj.toString(), 0);
    }

    /**
     * @description : Description of the method
     * 
     * @author songlin.li
     * @param obj
     * @return
     */
    public static Double toDouble(Object obj) {
        if (obj instanceof BigDecimal)
            return BigDecimal2Double((BigDecimal) obj);
        return toDouble(obj.toString(), 0);
    }

    public static void main(String[] args) {
        System.out.println(divide(11111, 1024, 2));
    }
}
